-- absorb
-- created by panyl
-- 技能: 吸能术，附带吸血效果
-- 格式：absorb(威力, 上限, 回合数)

return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = -1;

        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[3] > 0 then
            -- 不包括本回合
            effectRound = round + para[3] + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        -- 最大效果
        local max = para[2];

        -- 威力
        local force = 1000;
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        --最终威力
        force = math.modf(force * (addon_rate + 1000)/1000);

        -- 魔力
        local magic = source:getMagic();

        -- 效果(千分比)
        local v = math.modf((1000 * magic + (magic + 18) * (force - 1000)) / (magic + 18));
        v = math.modf(para[1] * v / force);
        v = math.min(v, max);

        local condition = {
            ["value"] = v,
            ["end_round"] = effectRound,
        };

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 附加状态
        CombatStatusM.applyStatus(target, "absorb", condition);

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 1. 持续回合
        local baseRound = para[3];
        local round = para[3];

        -- 增加持续回合的效果
        local prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        -- 2. 效果
        -- 最大效果
        local max = para[2];

        -- 威力
        local base = 1000;
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 魔力
        local magic = source:getMagic();

        -- 效果(百分比）
        local vbase = math.modf(para[1] / 10 * math.modf((1000 * magic + (magic + 18) * (base - 1000)) / (magic + 18)) / base);
        local vforce = math.modf(para[1] / 10 * math.modf((1000 * magic + (magic + 18) * (force - 1000)) / (magic + 18)) / force);
        vforce = math.min(vforce, max / 10);
        --vbase = math.min(vbase, max / 10);
        --local vaddon = vforce - vbase;
        desc = string.gsub(desc, "{absorb}", vforce);

        prop = PropM.combine(ME.user, "magic_revive", skillId);
        local mp = PropM.apply(prop, 1000);
        if mp > 0 then
            desc = string.gsub(desc, "{absorb_mp}", mp);
        end

        return desc;
    end,
};
